﻿using Google.Protobuf;
using MP_Game.Protos.V1;
using MP_Network;
using System.Collections.Concurrent;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace TestClient
{
    internal class TestClient
    {
        static async Task Main(string[] args)
        {

            NetClient client = new NetClient(NetConst.IPHost, NetConst.CenterServerPort, CliType.Player);
            client.StartConnect();

            while (true)
            {
                Thread.Sleep(1);
            }

            //await RegistNewAcct($"account", "1234");
        }

        private static async Task RegistNewAcct(string acct, string pwd)
        {
            // 构造请求
            var req = new RegistReq
            {
                Acct = acct,
                Pwd = pwd
            };

            // 二进制序列化
            byte[] body;
            using (var ms = new MemoryStream())
            {
                req.WriteTo(ms);
                body = ms.ToArray();
            }

            // 发POST请求
            using var http = new HttpClient
            {
                BaseAddress = new Uri("http://localhost")
            };
            var content = new ByteArrayContent(body);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/x-protobuf");

            LogMsg.Info("Sending regist request...");
            var resp = await http.PostAsync("/regist", content);

            resp.EnsureSuccessStatusCode();

            using var rspStream = await resp.Content.ReadAsStreamAsync();
            var rsp = LoginRsp.Parser.ParseFrom(rspStream);

            LogMsg.Info($"Response:{rsp}");
        }
    }
}
